﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using DTO;

namespace DAO
{
    public class LoaiThietBiDAO
    {
        public List<LoaiThietBiDTO> LayDanhSachLoaiThietBi()
        {
            List<LoaiThietBiDTO> ds = new List<LoaiThietBiDTO>();
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cm = new SqlCommand();
            cm.Connection = conn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "usp_LayDanhSachLoaiThietBi";
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                LoaiThietBiDTO dto = new LoaiThietBiDTO();
                dto.MaLoaiThietBi = dr.GetInt32(0);
                dto.TenLoaiThietBi = dr.GetString(1);
                ds.Add(dto);
            }
            dr.Close();
            conn.Close();
            return ds;
        }

        public LoaiThietBiDTO LayThongTinLoaiThietBi(int ma)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cm = new SqlCommand();
            cm.Connection = conn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "usp_LayThongTinThietBi";                     
            cm.Parameters.Add("@MaLoaiThietBi", SqlDbType.Int).Value = ma;
            SqlDataReader dr = cm.ExecuteReader();
            LoaiThietBiDTO dto = new LoaiThietBiDTO();
            dr.Read();
            dto.MaLoaiThietBi = dr.GetInt32(0);
            dto.TenLoaiThietBi = dr.GetString(1);
            dr.Close();
            conn.Close();
            return dto;
        }

        public string LayTenLoaiThietBiTheoMa(int ma)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cm = new SqlCommand();
            cm.Connection = conn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "usp_LayTenLoaiThietBiTheoMa";
            cm.Parameters.Add("@MaLoaiThietBi", SqlDbType.Int).Value = ma;
            string ten = cm.ExecuteScalar().ToString();
            conn.Close();
            return ten;
        }

        public void ThemLoaiThietBi(string ten)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cm = new SqlCommand();
            cm.Connection = conn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "usp_ThemLoaiThietBi";
            cm.Parameters.Add("@TenLoaiThietBi", SqlDbType.NVarChar).Value = ten;
            cm.ExecuteNonQuery();
            conn.Close();
        }

        public void XoaLoaiThietBi(int ma)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cm = new SqlCommand();
            cm.Connection = conn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "usp_XoaLoaiThietBi";
            cm.Parameters.Add("@MaLoaiThietBi", SqlDbType.Int).Value = ma;
            cm.ExecuteNonQuery();
            conn.Close();
        }

        public void CapNhatLoaiThietBi(LoaiThietBiDTO dto)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cm = new SqlCommand();
            cm.Connection = conn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "usp_CapNhatLoaiThietBi";
            cm.Parameters.Add("@MaLoaiThietBi", SqlDbType.Int).Value = dto.MaLoaiThietBi;
            cm.Parameters.Add("@TenLoaiThietBi", SqlDbType.NVarChar).Value = dto.TenLoaiThietBi;
            cm.ExecuteNonQuery();
            conn.Close();
        }
    }
}
